<?php
include './header.php';

$limit = 3;
$size = 5;
$current = (!empty($_GET['page'])) ? $_GET['page'] : 1;
$offset = ($current - 1) * $limit;

if (!empty($_GET['news_type'])) {
  $type = $_GET['news_type'];
  $condition = "WHERE news_type = {$type} LIMIT {$offset},{$limit}";
  $news = $db->selectAll('news', '*', $condition);
  $count = $db->selectOne('news', 'COUNT(*)', $condition)[0];
} else {
  $condition = "LIMIT {$offset},{$limit}";
  $news = $db->selectAll('news', '*', $condition);
  $count = $db->selectOne('news', 'COUNT(*)')[0];
}
// pre($news);die;
$page = page($current, $count, $limit, $size, 'pagination');

// 删除
if (!empty($_GET['del_news_id'])) {
  $del_id = $_GET['del_news_id'];
  $del_cond = "WHERE news_id = $del_id";
  $db->delData('news', $del_cond);
}

// 批量删除
if ($_POST['del_all']) {
  for ($i = 0; $i < count($_POST['idarr']); $i++) {
    $del_cond = "WHERE news_id = {$_POST['idarr'][$i]}";
    $db->delData('news', $del_cond);
  }
}


?>

<!-- Start: Content -->
<section id="content">
  <div id="topbar" class="affix">
    <ol class="breadcrumb">
      <li><a href="#"><span class="glyphicon glyphicon-home"></span></a></li>
      <li class="active">新闻管理</li>
    </ol>
  </div>
  <div class="container">

    <div class="row">
      <div class="col-md-12">
        <div class="panel">
          <div class="panel-heading">
            <div class="panel-title">新闻列表</div>
            <a href="news_add.php" class="btn btn-info btn-gradient pull-right"><span class="glyphicons glyphicon-plus"></span> 添加文章</a>
          </div>
          <form action="" method="post">
            <div class="panel-body">
              <h2 class="panel-body-title">互联网</h2>
              <table class="table table-striped table-bordered table-hover dataTable">
                <tr class="active">
                  <th class="text-center" width="100"><input type="checkbox" value="" id="checkall" class=""> 全选</th>
                  <th width="150px">标题</th>
                  <th>发布时间</th>
                  <th width="150px">图片</th>
                  <th>作者</th>
                  <th width="200">操作</th>
                </tr>
                <?php foreach ($news as $val) { ?>
                  <tr class="success">
                    <td class="text-center"><input type="checkbox" value="<?php echo $val['news_id']; ?>" name="idarr[]" class="cbox"></td>
                    <td><?php echo $val['news_title']; ?></td>
                    <td><?php echo date('Y-m-d', $val['news_time']); ?></td>
                    <td><img src="<?php echo $val['news_thumb']; ?>" alt=""></td>
                    <td><?php echo $val['news_author']; ?></td>
                    <td>
                      <div class="btn-group">
                        <a href="news_edit.php?news_id=<?php echo $val['news_id']; ?>" class="btn btn-default btn-gradient"><span class="glyphicons glyphicon-pencil"></span></a>
                        <a onclick="return confirm('确定要删除吗？');" href="news_list.php?del_news_id=<?php echo $val['news_id']; ?>" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicons glyphicon-trash"></span></a>
                      </div>
                    </td>
                  </tr>
                <?php } ?>
              </table>

              <div class="pull-left">
                <button type="submit" class="btn btn-default btn-gradient pull-right delall" name="del_all" value="del_all"><span class="glyphicons glyphicon-trash"></span></button>
              </div>

              <?php echo $page; ?>

              <!-- <div class="pull-right">
                    <ul class="pagination" id="paginator-example">
                      <li><a href="#">&lt;</a></li>
                      <li><a href="#">&lt;&lt;</a></li>
                      <li><a href="#">1</a></li>
                      <li class="active"><a href="#">2</a></li>
                      <li><a href="#">3</a></li>
                      <li><a href="#">&gt;</a></li>
                      <li><a href="#">&gt;&gt;</a></li>
                    </ul>
                  </div> -->

            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</section>
<!-- End: Content -->
</div>
<!-- End: Main -->
</body>

</html>